<!DOCTYPE html>
<html lang="${LANG}">

<head>
    <meta charset="UTF-8" />
    <title>QBT_TR(New Category)QBT_TR[CONTEXT=TransferListWidget]</title>
    <link rel="stylesheet" href="css/style.css?v=${CACHEID}" type="text/css" />
    <script src="scripts/lib/mootools-1.2-core-yc.js"></script>
    <script src="scripts/lib/mootools-1.2-more.js"></script>
    <script src="scripts/misc.js?locale=${LANG}&v=${CACHEID}"></script>
    <script>
        'use strict';

        new Keyboard({
            defaultEventType: 'keydown',
            events: {
                'Enter': function(event) {
                    $('categoryNameButton').click();
                    event.preventDefault();
                },
                'Escape': function(event) {
                    window.parent.closeWindows();
                    event.preventDefault();
                },
                'Esc': function(event) {
                    window.parent.closeWindows();
                    event.preventDefault();
                }
            }
        }).activate();

        window.addEvent('domready', function() {
            const uriAction = window.qBittorrent.Misc.safeTrim(new URI().getData('action'));
            const uriHashes = window.qBittorrent.Misc.safeTrim(new URI().getData('hashes'));
            const uriCategoryName = window.qBittorrent.Misc.safeTrim(new URI().getData('categoryName'));
            const uriSavePath = window.qBittorrent.Misc.safeTrim(new URI().getData('savePath'));

            if (uriAction === "edit") {
                if (!uriCategoryName)
                    return false;

                $('categoryName').set('disabled', true);
                $('categoryName').set('value', window.qBittorrent.Misc.escapeHtml(uriCategoryName));
                $('savePath').set('value', window.qBittorrent.Misc.escapeHtml(uriSavePath));
                $('savePath').focus();
            }
            else {
                $('categoryName').focus();
            }

            $('categoryNameButton').addEvent('click', function(e) {
                new Event(e).stop();

                const savePath = $('savePath').value.trim();
                const categoryName = $('categoryName').value.trim();

                const verifyCategoryName = function(name) {
                    if ((name === null) || (name === ""))
                        return false;
                    if (name.match("^([^\\\\\\/]|[^\\\\\\/]([^\\\\\\/]|\\/(?=[^\\/]))*[^\\\\\\/])$") === null) {
                        alert("QBT_TR(Invalid category name:\nPlease do not use any special characters in the category name.)QBT_TR[CONTEXT=HttpServer]");
                        return false;
                    }
                    return true;
                };

                switch (uriAction) {
                    case "set":
                        if ((uriHashes === "") || !verifyCategoryName(categoryName))
                            return;

                        new Request({
                            url: 'api/v2/torrents/createCategory',
                            method: 'post',
                            data: {
                                category: categoryName,
                                savePath: savePath
                            },
                            onSuccess: function() {
                                new Request({
                                    url: 'api/v2/torrents/setCategory',
                                    method: 'post',
                                    data: {
                                        hashes: uriHashes,
                                        category: categoryName
                                    },
                                    onComplete: function() {
                                        window.parent.closeWindows();
                                    }
                                }).send();
                            },
                            onFailure: function() {
                                alert("QBT_TR(Unable to create category)QBT_TR[CONTEXT=HttpServer] " + window.qBittorrent.Misc.escapeHtml(categoryName));
                            }
                        }).send();
                        break;
                    case "create":
                        if (!verifyCategoryName(categoryName))
                            return;

                        new Request({
                            url: 'api/v2/torrents/createCategory',
                            method: 'post',
                            data: {
                                category: categoryName,
                                savePath: savePath
                            },
                            onComplete: function() {
                                window.parent.closeWindows();
                            }
                        }).send();
                        break;
                    case "edit":
                        new Request({
                            url: 'api/v2/torrents/editCategory',
                            method: 'post',
                            data: {
                                category: uriCategoryName, // category name can't be changed
                                savePath: savePath
                            },
                            onComplete: function() {
                                window.parent.closeWindows();
                            }
                        }).send();
                        break;
                }
            });
        });
    </script>
</head>

<body>
    <div style="padding: 10px 10px 0px 10px;">
        <p style="font-weight: bold;">QBT_TR(Category)QBT_TR[CONTEXT=TransferListWidget]:</p>
        <input type="text" id="categoryName" style="width: 220px;" />
        <p style="font-weight: bold;">QBT_TR(Save path)QBT_TR[CONTEXT=TransferListWidget]:</p>
        <input type="text" id="savePath" style="width: 220px;" />
        <div style="text-align: center; padding-top: 10px;">
            <input type="button" value="QBT_TR(Add)QBT_TR[CONTEXT=HttpServer]" id="categoryNameButton" />
        </div>
    </div>
</body>

</html>
